package com.zx.core.utils;

import com.alibaba.fastjson2.JSON;
import com.zx.core.wrapper.ResponseWrapper;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;

import java.nio.charset.StandardCharsets;

/**
 * <p>
 * description: 客户端工具类  <br>
 * create: 2024-08-15 23:41
 * </p>
 *
 * @author zhou  xun
 */
public class ServletUtils {

    /**
     * 响应缓冲处理
     *
     * @param result            {@link ResponseWrapper}
     * @param dataBufferFactory {@link DataBufferFactory}
     * @return {@link DataBuffer}
     * @author zhou  xun
     * @since 2024-08-15
     */
    public static <T extends ResponseWrapper> DataBuffer getDataBuffer(T result, DataBufferFactory dataBufferFactory) {
        byte[] responseBody = JSON.toJSONString(result).getBytes(StandardCharsets.UTF_8);
        return dataBufferFactory.wrap(responseBody);
    }
}
